package com.example.demo.handler;

import com.example.demo.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginHandler implements HandlerInterceptor {
    //在请求之前要执行的方法
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //false 不放行请求
        //true 才放行
        User user =(User) request.getSession().getAttribute("user");
        if(user == null){
            response.sendRedirect("/login");
            return false;
        }else{
            return  true;
        }
    }
}
